import { defineStore } from 'pinia'
import type { IClub, IClubMember, IClubState } from './type'
import {
  deleteClubById,
  getAllClubs,
  getClubById,
  getClubByName,
  getClubMemberByClubId,
  postCreateClub,
  reviewMemberById,
  updateClub,
  deleteMemberById,
  updateClubStatus,
  getClubByUserId,
  joinData
} from '@/service/main/club/club'

const useClubStore = defineStore('club', {
  state: (): IClubState => ({
    clubsList: [],
    clubsTotalCount: 0,
    clubDetail: {} as IClub,
    clubMemebrs: [],
    clubMemebersTotalCount: 0,
    clubJoinedData: []
  }),
  actions: {
    // 获取全部社团信息
    async getClubsListAction(limit = 10, offset = 0, status?: string) {
      const clubsListResult = await getAllClubs(limit, offset, status)
      const { totalCount, list } = clubsListResult.data
      this.clubsList = list
      this.clubsTotalCount = totalCount
    },

    // 根据社团名称获取社团
    async getClubListByNameAction(
      name: string,
      limit = 10,
      offset = 0,
      status?: string
    ) {
      const clubsListResult = await getClubByName(name, limit, offset, status)
      const { totalCount, list } = clubsListResult.data
      this.clubsList = list
      this.clubsTotalCount = totalCount
    },

    // 创建社团
    async newClubDataAction(clubInfo: any) {
      const newResult = await postCreateClub(clubInfo)
      this.getClubsListAction()
    },

    // 删除社团
    async deleteClubByIdAction(id: number) {
      const deleteResult = await deleteClubById(id)
      this.getClubsListAction()
      return deleteResult
    },

    // 更新社团
    async editClubDataAtion(id: number, clubInfo: any) {
      const editResult = await updateClub(id, clubInfo)
      this.getClubsListAction()
    },

    // 更新社团状态
    async updateClubStatusAction(id: number, status: string) {
      const result = await updateClubStatus(id, status)
    },

    /** detail部分 */
    // 根据id获取社团详情
    async getClubDetaiByIdAction(id: number) {
      const detailResult = await getClubById(id)
      this.clubDetail = detailResult.data
    },
    // 获取社团成员的详情
    async getClubMemberByClubIdAction(
      id: number,
      limit?: number,
      offset?: number,
      name?: string,
      status?: string
    ) {
      const clubMembersResult = await getClubMemberByClubId(
        id,
        limit,
        offset,
        name,
        status
      )
      const { totalCount, list } = clubMembersResult.data
      this.clubMemebrs = list.filter(
        (item: IClubMember) => item.user_id !== this.clubDetail.leader_id
      )
      this.clubMemebersTotalCount = totalCount
    },
    // 更改成员状态
    async setClubMemberStatusAction(
      club_id: number,
      id: number,
      status?: string
    ) {
      await reviewMemberById(club_id, id, status)
      this.getClubsListAction()
    },
    // 删除成员
    async deleteClubMemebrAction(club_id: number, user_id: number) {
      await deleteMemberById(club_id, user_id)
      this.getClubsListAction()
    },
    // 根据用户id获取社团
    async getClubByUserIdAction() {
      const res = await getClubByUserId()
      this.clubsList = res.data
      this.clubsTotalCount = 0
    },

    async joinDataAction() {
      const res = await joinData()
      this.clubJoinedData = res.data
    }
  }
})

export default useClubStore
